/*

0.0.6 Jquery needed.
0.0.5 flash2js.info, flash2js.param 可使用 Ture/False 控制回傳為 String 或 Object. 方便內部測試
0.0.4 成功由 Javascript => Actionscript 並且回傳 Flash 內部函數
0.0.3 完成 callJsFn , 可於 as 內隨意加長附帶的 arguments. 已測試 string, number, boolean
0.0.2 完成 assignJsVar
0.0.1 Flash to javascript 可以使用 Object 帶參數, 保留大部份的關係及名稱
0.0.0 媽的 Adobe 的 local 一定要記得去 adobe 官網把 local 運行被永遠準許. 每次upload 上去server測試太麻煩了, 還有 DNS cache. 天殺的 adobe....

Request:
swfobject.js	// Download form http://code.google.com/p/swfobject/
JQuery.js		// Download form http://jquery.com/

Usage:
flash2js.setTarget(You_Flash_ID);
flash2js.target	<- you can call flash function by this.
flash2js.info(true/false); <- Flash file info. build-in flash side function

// you can call flash side registration's function.
flash2js.target.YOUR_FLASH_FUNCTION(_someValue, _arg1, _arg2....);
*/
/*** Uncomment this for test **********************
// 
// Usage : Add follow command in FLASH:
//		cache.global().address('javascript').callJS('TESTFN','This String send form Flash with',true,12345678,'I am String');

// Javascript part
function TESTFN(){
	var str="";
	var arg = arguments;
	for(var i=0;i<arg.length;i++){
		str += "Arg["+i+"] is ("+typeof(arg[i])+") = "+ arg[i]+" \r\t";
	}//for
	alert("TESTFN : \r\t"+ str);
}//fn
//*************************************************/


// init
$(document).ready(function(){
	flash2js.isReady=true;
});
// swfobject.addLoadEvent(function(){flash2js.isReady=true});	//  While SwfObject onload, the var will be TRUE;

var flash2js = new Object();
flash2js.target=null;
flash2js.isReady=false; // onload will be "true";
flash2js.connection= function(){ return flash2js.isReady;}	// flash will ping this function to get result.
flash2js.assignJsVar= function(_obj){
	eval(_obj.name +" = "+ _obj.value);
	// alert("assignJsVar - received following :\r\t"+ _obj.name +" = "+ _obj.value);
}
flash2js.callJsFn = function(_obj){
	var _str = "";
	for(var i=0;i<_obj.arg.length;i++){
		_str += fixType(_obj.arg[i]);
		if( (i+1) < _obj.arg.length) _str+=",";
	}//for
	//alert("callJsFn:\n received following :\r\t"+_obj.fn+"("+_str+")");
	return eval(_obj.fn+"("+_str+")");
}//fn
flash2js.param = function(astxt){
	if(astxt===undefined) astxt=true;
	var flashparam = flash2js.target;
	var temp ="";
	if(astxt){
		for(var key in flashparam) {
		   temp += key + ": " + flashparam[key] + "\n";
		}//for
		return "Flash params : "+ flashparam +"\n"+temp;
	}else{
		return flashparam;
	}//if
}//fn
flash2js.info = function(astxt){
	if(astxt===undefined) astxt=true;
	var flashInfo = flash2js.target.getFlashInfo();
	var temp ="";
	if(astxt){
		for(var key in flashInfo) {
		   temp += key + ": " + flashInfo[key] + "\n";
		}//for
		return "Flash Info : "+ flashInfo +" \n"+temp;
	}else{
		return flashInfo;
	}//fn
}//fn

flash2js.setTarget=function(movieName){
	flash2js.target=getflash(movieName);
	if(flash2js.target==undefined){
		flash2js.target=$("#"+movieName).get(0);
	}//if
}//fn
function getflash(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {return window[movieName]}
	else {return document[movieName]}
}//fn
function fixType(_input){
	switch(typeof(_input)){
		case "string": return "'"+_input+"'"; break;
		case "number":
		case "boolean":
		case "object":
		case "function": return _input; break;
		case "NaN":
		case "undefined":
		default:
			return "undefined";
		break;
	}//case
}//fn